﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
using System.Collections;
using System.Web;


namespace kxcxw.Business
{
    public static class ImgUpLoadHelper
    {
        #region  存储为相应大小的图片
        /// <summary>
        /// 存储为相应大小的图片
        /// </summary>
        /// <param name="path">文件夹路径</param>
        /// <param name="thumbnail_id">图片ID</param>
        /// <param name="target_width">宽度</param>
        /// <param name="target_height">高度</param>
        /// <param name="jpeg_image_upload">HttpPostFileBase</param>
        public static string SavePicFormat(string path, string thumbnail_id, int target_width, int target_height, HttpPostedFileBase jpeg_image_upload)
        {
            System.Drawing.Image original_image = null;
            System.Drawing.Bitmap final_image = null;
            System.Drawing.Graphics graphic = null;
            try
            {
                original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
                int width = original_image.Width;
                int height = original_image.Height;
                int new_width, new_height;
                float target_ratio = (float)target_width / (float)target_height;
                float image_ratio = (float)width / (float)height;

                if (target_ratio > image_ratio)
                {
                    new_height = target_height;
                    new_width = (int)Math.Floor(image_ratio * (float)target_height);
                }
                else
                {
                    new_height = (int)Math.Floor((float)target_width / image_ratio);
                    new_width = target_width;
                }
                new_width = new_width > target_width ? target_width : new_width;
                new_height = new_height > target_height ? target_height : new_height;

                final_image = new System.Drawing.Bitmap(target_width, target_height);
                string url = path;
                graphic = System.Drawing.Graphics.FromImage(final_image);
                graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), new System.Drawing.Rectangle(0, 0, target_width, target_height));
                int paste_x = (target_width - new_width) / 2;
                int paste_y = (target_height - new_height) / 2;
                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; /* new way */

                graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height);
                if (thumbnail_id.Contains("."))
                    final_image.Save(url + thumbnail_id);
                else
                    final_image.Save(url + thumbnail_id + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                //Response.StatusCode = 200;
                return thumbnail_id;
            }
            catch (Exception ex)
            {
                return "";
            }
            finally
            {
                // Clean upF:\KXCXW_Project\KXCXW\KXCXW.Web\Content\swfupload\handlers.js
                if (final_image != null) final_image.Dispose();
                if (graphic != null) graphic.Dispose();
                if (original_image != null) original_image.Dispose();
            }

        }
        #endregion
    }
}
